package idazkaria;

import hasiera_pantaila.DBKudeatzailea;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import com.toedter.calendar.JCalendar;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EI_Hitzordua_sortu_1_3_5 extends JFrame {

	private JPanel contentPane;
	private JTextField bezeroa;
	private JTextField terapeuta;
	private JComboBox terapia;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EI_Hitzordua_sortu_1_3_5 frame = new EI_Hitzordua_sortu_1_3_5();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EI_Hitzordua_sortu_1_3_5() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 579, 390);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblHitzorduaSortu = new JLabel("HITZORDUA SORTU:");
		lblHitzorduaSortu.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblHitzorduaSortu.setBounds(23, 11, 210, 14);
		contentPane.add(lblHitzorduaSortu);
		
		JLabel lblBezeroa = new JLabel("Bezeroaren NAN-a");
		lblBezeroa.setBounds(23, 44, 106, 14);
		contentPane.add(lblBezeroa);
		
		bezeroa = new JTextField();
		bezeroa.setBounds(147, 41, 139, 20);
		contentPane.add(bezeroa);
		bezeroa.setColumns(10);
		
		JLabel lblTerapeuta = new JLabel("Terapeutaren NAN-a");
		lblTerapeuta.setBounds(23, 75, 122, 14);
		contentPane.add(lblTerapeuta);
		
		terapeuta = new JTextField();
		terapeuta.setBounds(144, 72, 89, 20);
		contentPane.add(terapeuta);
		terapeuta.setColumns(10);
		
		JLabel lblTerapia = new JLabel("Terapia");
		lblTerapia.setBounds(23, 110, 56, 14);
		contentPane.add(lblTerapia);
		
		terapia = new JComboBox();
		terapia.setModel(new DefaultComboBoxModel(new String[] {"Akupuntura", "Yoga saioak", "Moxibustion", "Pilates saioak"}));
		terapia.setBounds(72, 107, 106, 20);
		contentPane.add(terapia);
		
		
			
		
		
		JLabel lblAgenda = new JLabel("Agenda");
		lblAgenda.setBounds(240, 110, 56, 14);
		contentPane.add(lblAgenda);
		
		//egutegia panel batean sartu dut. AGENDA etiketa azpian jartzeko, setBounds metodoaren zenbakiak aldatu ditut.
		JPanel calendar=new JPanel();
		calendar.setBounds(286, 112, 251, 177);
		this.contentPane.add(calendar);
		calendar.add(new JCalendar());
		
		JButton btnGorde = new JButton("Gorde");
	btnGorde.addActionListener(new ActionListener() {
			
			protected boolean zerbaitIdatziDu(){
					return bezeroa.getText().length() > 0 && terapeuta.getText().length()>0;
			                    }
			//terapia mota ezartzen da
			protected String terapiaMota(){
				
				if (terapia.getSelectedItem().equals("Akupuntura")){
					return "Akupuntura";}
					else {
				if (terapia.getSelectedItem().equals("Yoga saioak")){
						return "Yoga saioak";	}
				else {
				if (terapia.getSelectedItem().equals("Moxibustion")){
					
					return "Moxibustion";}	
					else{
						
					 return "Pilates saioak";}}}
				}
			
				
			
			public void actionPerformed(ActionEvent arg0) {
				 try
	                {
			
				if (!zerbaitIdatziDu()){
                    JOptionPane.showMessageDialog(null, "Ezin da eremua hutsik egon");
                    //hemen gertatzen dena da agenda ez dugula begiratu(jcalendar)
	//			}else if (!existitzenDa(agenda.getText())  ){					
					// datubasean ordu berria txertatu
					/*HitzorduaSortu(bezeroa.getText(), terapeuta.getText(), agenda, terapiaMota());
					 //sortu mezu interfaz bat
				EI_Hitzordua_sortuta_mezua_1_3_5_1 mezua=EI_Hitzordua_sortuta_mezua_1_3_5_1();
				//ipini true eta gero kendu oraingo pantaila
				mezua.setVisible(true);
				EI_Hitzordua_sortu_1_3_5.this.setVisible(false);*/
				}
			}
			catch (Exception e){
                e.printStackTrace();
            }
		
			  }
    	});
		
		btnGorde.setBounds(161, 300, 89, 23);
		contentPane.add(btnGorde);
		
		JButton btnAtzera = new JButton("Atzera");
		btnAtzera.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EI_Bezeroaren_informazioa_maneiatu_1_3 bat=new EI_Bezeroaren_informazioa_maneiatu_1_3();
				bat.setVisible(true);
				EI_Hitzordua_sortu_1_3_5.this.setVisible(false);}
			});
		btnAtzera.setBounds(259, 300, 89, 23);
		contentPane.add(btnAtzera);
		
		JLabel lblAdibv = new JLabel("Adib: 11111111v");
		lblAdibv.setBounds(303, 44, 115, 14);
		contentPane.add(lblAdibv);
		this.setVisible(true);
	}
	public void HitzorduaSortu(String a, String c,  String e, String d) { 
    
    		/*SD begiratuta: K1: INSERT INTO hitzordua (izena, kodea, terapeuta, ordutegia, prezioa, deskribapena )
VALUES(%izena, %kodea, %terapeuta, % ordutegia, %prezioa, %deskribapena)*/
    		
    		DBKudeatzailea dbk = DBKudeatzailea.getInstantzia();
    	
    	
    		String k1 ="INSERT INTO terapia (`bezeroNAN`, `terapeutaNAN`, `agenda`, `terapia`) " +
    				" VALUES ('" + bezeroa.getText()  + "', '" + terapeuta.getText()  + "',,'" + terapia.getSelectedItem() + "')";        
    		
    		ResultSet rs =  dbk.execSQL(k1);				
    		
    		 rs =  dbk.execSQL(k1);
		

    		try {
    			rs.next();
    			rs.close();
    			System.out.println("Ondo txertatu da");
    			
    		} catch (SQLException e1) {
    			System.out.println("Gaizki txertatu da");
    		}
    		
    		

    }
	
	public boolean existitzenDa(String Data) {
		DBKudeatzailea dbk = DBKudeatzailea.getInstantzia();
		String k1 =("SELECT * FROM hitzordua WHERE agenda='"+Data+"'");

		ResultSet rs =  dbk.execSQL(k1);
		try {
			rs.next();
			System.out.println(rs.getString(0));
			rs.close();
			System.out.println("aurkituta");
			return true;

		} catch (SQLException e) {
			System.out.println("ez aurkituta");
			return false;
		}
	}
}
